package com.wanmait.computershop.webController;

import com.wanmait.computershop.dao.UserDAO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("web")
public class WebRegisterController {

    //处理访问注册页面的方法
    @GetMapping("register")
    public String register() {
        return "web/register";
    }

    //处理注册请求的方法
    @PostMapping("/doRegister")
    @ResponseBody
    public Integer doRegister(String username, String userpass) {
        try {
            // 验证用户名和密码是否为空
            if (username == null || userpass == null) {
                return 4000;  // 参数错误返回4000
            }

            UserDAO dao = new UserDAO();
            dao.insert(username, userpass);
            return 200;  // 成功返回200

        } catch (Exception e) {
            e.printStackTrace();
            return 4000;  // 失败返回4000
        }
    }
}
